import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '../views/HomeView.vue'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'main',
    component: () => import('../views/Main.vue'),
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'home',
        component: HomeView
      },
      {
        path: '/company',
        name: 'company',
        children: [
          {
            path: '/company/companyInfo',
            name: 'companyInf',
            component: () => import('../views/company/CompanyInf.vue')
          },
          {
            path: '/company/list',
            name: 'companyList',
            component: () => import('../views/company/CompanyList.vue')
          },
          {
            path: '/company/shake',
            name: 'companyDetail',
            component: () => import('../views/company/CompanyShake.vue')
          },
          {
            path: '/company/edit',
            name: 'companyEdit',
            component: () => import('../views/company/CompanyEdit.vue')
          },
          {
            path: '/company/electric',
            name: 'companyElectric',
            component: () => import('../views/company/CompanyElectric.vue')
          }
        ]
      },
      {
        path: '/dataQuery',
        name: 'dataQuery',
        children: [
          {
            path: '/dataQuery/dataQuery',
            name: 'dataQuery',
            component: () => import('../views/dataQuery/DataQuery.vue')
          },
          {
            path: '/dataQuery/elecQuery',
            name: 'elecQuery',
            component: () => import('../views/dataQuery/ElecQuery.vue')
          },
        ]
      },
      {
        path: '/control',
        name: 'control',
        children: [
          {
            path: '/control/controlAll',
            name: 'controlAll',
            component: () => import('../views/control/ControlAll.vue')
          },
          {
            path: '/control/weather',
            name: 'Weather',
            component: () => import('../views/control/Weather.vue')
          },
          {
            path: '/control/money',
            name: 'money',
            component: () => import('../views/control/Money.vue')
          }
        ]
      },
      {
        path: '/alert',
        name: 'alert',
        children: [
          {
            path: '/alert/data',
            name: 'alertData',
            component: () => import('../views/alert/AlertData.vue')
          },
          {
            path: '/alert/setting',
            name: 'alertSetting',
            component: () => import('../views/alert/AlertSetting.vue')
          },
          {
            path: '/alert/ignore',
            name: 'alertIgnore',
            component: () => import('../views/alert/AlertIgnore.vue')
          }
        ]
      },
      {
        path: '/dataReport',
        name: 'dataReport',
        component: () => import('../views/DataReport.vue')
      },
      {
        path: '/onlineReport',
        name: 'onlineReport',
        component: () => import('../views/OnlineReport.vue')
      },
      {
        path: '/systemControl',
        name: 'systemControl',
        component: () => import('../views/SystemControl.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  // 路由拦截
  let isLogin = localStorage.getItem('Authorization1');
  if (!isLogin && to.path !== '/login') {
    next('/login')
  }
  else next() // 跳转到下一个路由
})

export default router
